layui.define(['laytpl', 'helper'], function (exports) {
	window.$ = layui.$;
	var laytpl=layui.laytpl;
	var obj = {
		//初始化渲染视图
		init: function(url, successCallback){
			ajax_request(url, 'GET', '', function(res){
					//渲染
					var getTpl = document.getElementById('view').innerHTML;
					var view = document.getElementById('view');
					var data = res.data;
					laytpl(getTpl).render(data, function(html){
						
						  view.innerHTML = html;
						  
						  //渲染成功回调
						  if(successCallback != undefined){
							successCallback(res);
						  }
						  
					});
					
				},function(res){
					if(res.errorcode == 10001){
						sessionStorage.clear();
						if(strrpos(parent.window.location.href, "/#/") >= 0){
							return window.location = appRoute.login+"?referer="+window.btoa(parent.window.location.href);
						}
						window.location = appRoute.login;
					}else if(res.errorcode == 404){
						page404();
					}else if(res.errorcode == 500){
						page500();
					}else{
						noticeMsg('error', res.errormsg);
					}
					throw "接口异常"+json_encode(res);
				}
			);
		}
	}	
	window.page404 = function(){
		$("#view").html('<iframe scrolling="auto" allowtransparency="true" src="'+rootPath+'../../page/404.html'+'" style="height: 600px;width:100%;" frameborder="0"></iframe>');
	}
	window.page500 = function(){
		$("#view").html('<iframe scrolling="auto" allowtransparency="true" src="'+rootPath+'../../page/404.html'+'" style="height: 600px;width:100%;" frameborder="0"></iframe>');
	}
	/**
	 * 模版foreach函数
	 * @param {Object} arr
	 * @param {Object} fun
	 */
	window.arr_foreach = function(arr, fun){
		var html = '';
		for(i in arr){
			html += fun(i, arr[i]);
		}
		return html;
	}
	/**
	 * 模版input格式化时间
	 * @param {Object} text
	 */
	window.input_time = function(text){
		if(is_numeric(text)){
			if(text > 0){
				text = formatDate(text, 'Y-M-D H:I:S');
			}
		}else if(text == null){
			text = '';
		}
		return text;
	}
	/**
	 * 模版input格式化日期
	 * @param {Object} text
	 */
	window.input_date = function(text){
		if(is_numeric(text)){
			if(text > 0){
				text = formatDate(text, 'Y-M-D');
			}
		}else if(text == null){
			text = '';
		}
		return text;
	}
	/**
	 * 模版默认值处理
	 * @param {Object} text
	 */
	window.input_default = function(text){
		if(text == 'null' || text == null){
			text = '';
		}
		return text;
	}
	/**
	 * 模版打印
	 * @param {Object} text
	 */
	window.var_dump = function(text){
		console.log(text);
		return text;
	}
	/**
	 * 是否避开浏览器自动排序的数组格式
	 * @param {Object} text
	 */
	window.is_avoidBrowser = function(text){
		if(text.hasOwnProperty('key')){
			return true;
		}
		return false;
	}
	exports('view', obj);//导出
});